package com.zhiyou.app.shop.website.action.stock;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.dao.pojo.UserStock;
import com.zhiyou.app.shop.website.dao.pojo.UserStockInfo;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IPlayService;
import com.zhiyou.app.shop.website.service.IUserAccountService;
import com.zhiyou.app.shop.website.service.IUserManagerService;
import com.zhiyou.app.shop.website.service.IUserStockService;

public class StockAction extends BaseAction {

	private static final long serialVersionUID = -1942120291511744346L;

	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private IPlayService playService;
	@Autowired
	IUserManagerService userManagerService;
	@Autowired
	IUserStockService userStockService;
	@Autowired
	IDicItemService dicItemService;
	
	private String cmd;
	private DicItem item;
	private String json;
	private String stockType;
	List<UserStock> stockList;
	
	public String initCallback(){
		return "callback";
	}
	
	public String callback(){
		if("callback".equalsIgnoreCase(cmd)){
			userStockService.callback(userids, stockType,per);
		}else if("search".equalsIgnoreCase(cmd)){
			stockList = userStockService.getByStockType(stockType);
		}
		return "callback";
	}
	
	public String stock(){
		List<DicItem> list = dicItemService.findByType("stock_flag");
		DicItem di = list.get(0);
		item = di;
		if(StringUtils.isNotBlank(cmd)){
			try{
				if(cmd.equalsIgnoreCase("resume")){
					di.setCode("1");
					dicItemService.update(di);
				}else if(cmd.equalsIgnoreCase("stop")){
					di.setCode("0");
					dicItemService.update(di);
				}else if(cmd.equalsIgnoreCase("buy")){
						userStockService.buy();
				}
				json = "1";
			}catch(Exception e){
				json = "0";
			}
			return "success";
		}else{
			return "operate";
		}
	}
	
	public String initAdd(){
		return "stockAdd";
	}
	
    public String stockAdd(){
    	if("stockAdd".equalsIgnoreCase(cmd)){
    		UserStock ustock = userStockService.findById(Integer.parseInt(userids));
			if(StringUtils.isNotBlank(stockType) && !stockType.endsWith(ustock.getStockType())){
                UserStock userStock = new UserStock();
                userStock.setUserId(ustock.getUserId());
                userStock.setBuyDate(new Date());
                userStock.setStockType(stockType);
                userStock.setStockNum(Long.valueOf(num));
                userStock.setRound(0);
                userStock.setPrice(String.valueOf(price));

                //这里是新加股票
                UserStockInfo usi = new UserStockInfo();
                usi.setCreateDate(new Timestamp(System.currentTimeMillis()));
                usi.setUserId(ustock.getUserId());
                usi.setPrice(new BigDecimal(ustock.getPrice()));
                usi.setStockQuantity(num);
                usi.setType("1");
                usi.setStatus("1");
                usi.setTradeDate(new Timestamp(System.currentTimeMillis()));
                //userStockInfoService.
                userStockService.save(userStock, usi);
            }else {
                ustock.setStockNum(ustock.getStockNum() + num);
                UserStockInfo usi = new UserStockInfo();
                usi.setCreateDate(new Timestamp(System.currentTimeMillis()));
                usi.setUserId(ustock.getUserId());
                usi.setPrice(new BigDecimal(ustock.getPrice()));
                usi.setStockQuantity(num);
                usi.setType("1");
                usi.setStatus("1");
                usi.setTradeDate(new Timestamp(System.currentTimeMillis()));
                //userStockInfoService.
                userStockService.save(ustock, usi);
            }
		}else if("search".equalsIgnoreCase(cmd)){
			stockList = userStockService.getByUserid(userId);
		}
        return "stockAdd";
    }
	
	private String userId;
	private float money;
	public String test(){
		System.out.println(userStockService.buyStock(userId,money));
		return "test";
	}
	
	
	
	public List<UserStock> getStockList() {
		return stockList;
	}

	public void setStockList(List<UserStock> stockList) {
		this.stockList = stockList;
	}

	public String getStockType() {
		return stockType;
	}

	public void setStockType(String stockType) {
		this.stockType = stockType;
	}

	public DicItem getItem() {
		return item;
	}

	public void setItem(DicItem item) {
		this.item = item;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	//以下两个方法是拆分
	private String splitType;//操作类型
	private Float per; //拆分比例
	public String initStockSplit(){
		return "init-split";
	}
	
	public String split(){
		userAccountService.splitStock(per);
		return "split-success";
	}

	//手动售出
	private String level;
	private String status;
	private Date starttime;
	private Date endtime;
	private int startnum;
	private int endnum;
	private float saleprice;
	private float saleprice1;
	private int num;
	private String userids;
	public String initSale(){
		return "initSale";
	}
	
	///
	public String listNum(){
		Page pg = getInitedPage();
		try{
			pg.setResult(userAccountService.listNum(userid, level, tradeType, startprice, endprice, status, 
					starttime, endtime, card, name, startnum, endnum, getInitedPage().getPageNo(),getInitedPage().getPageSize()));
		}catch(Exception e) {			
			e.printStackTrace();
		}
		setPage(pg);
		return initSale();
	}
	
	public String sale1(){
		String users[] = userids.split(",");
		try{
			for(String u:users){
				if(StringUtils.isNotBlank(u)){
					UserAccount ua = userAccountService.findUserAccount(u);
					User user = userManagerService.getUserByAccount(u);
					if(per>1){
						playService.doStockTrade(user.getUserId(),per.intValue(),new BigDecimal(saleprice),"1");
					}else{
						playService.doStockTrade(user.getUserId(),(int)(ua.getStockNum() * per),new BigDecimal(saleprice),"1");
					}
				}
			}
		}catch(Exception e) {			
			e.printStackTrace();
		}
		return "saleSuccess";
	}
	
	public String sale2(){
		String users[] = userids.split(",");
		try{
			for(String u:users){
				if(StringUtils.isNotBlank(u)){
					UserAccount ua = userAccountService.findUserAccount(u);
					User user = userManagerService.getUserByAccount(u);
					playService.doStockTrade(user.getUserId(),(int)(ua.getStockNum() - num),new BigDecimal(saleprice),"1");
				}
			}
		}catch(Exception e) {			
			e.printStackTrace();
		}
		return "saleSuccess";
	}
	
	//挂牌售出
	private String userid;
	private String tradeType;
	private float startprice;
	private float endprice;
	private String card;
	private String name;
	private float price;
	public String initSaleOrBuy(){
		return "initSaleOrBuy";
	}
	
	public String listSaleOrBuy(){
		Page pg = getInitedPage();
		try{
			pg.setResult(userAccountService.listSaleOrBuy(userid, tradeType, startprice, 
					endprice, card, name,getInitedPage().getPageNo(),getInitedPage().getPageSize()));
		}catch(Exception e) {			
			e.printStackTrace();
		}
		setPage(pg);
		return initSaleOrBuy();
	}
	
	//修改价格
	public String saleOrBuy(){
		//暂不作实际操作
		return "saleOrBuySuccess";
	}
	
	public String getSplitType() {
		return splitType;
	}

	public void setSplitType(String splitType) {
		this.splitType = splitType;
	}

	public Float getPer() {
		return per;
	}

	public void setPer(Float per) {
		this.per = per;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getTradeType() {
		return tradeType;
	}

	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}

	public float getStartprice() {
		return startprice;
	}

	public void setStartprice(float startprice) {
		this.startprice = startprice;
	}

	public float getEndprice() {
		return endprice;
	}

	public void setEndprice(float endprice) {
		this.endprice = endprice;
	}

	public String getCard() {
		return card;
	}

	public void setCard(String card) {
		this.card = card;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getStarttime() {
		return starttime;
	}

	public void setStarttime(Date starttime) {
		this.starttime = starttime;
	}

	public Date getEndtime() {
		return endtime;
	}

	public void setEndtime(Date endtime) {
		this.endtime = endtime;
	}

	public int getStartnum() {
		return startnum;
	}

	public void setStartnum(int startnum) {
		this.startnum = startnum;
	}

	public int getEndnum() {
		return endnum;
	}

	public void setEndnum(int endnum) {
		this.endnum = endnum;
	}

	public float getSaleprice() {
		return saleprice;
	}

	public void setSaleprice(float saleprice) {
		this.saleprice = saleprice;
	}

	public float getSaleprice1() {
		return saleprice1;
	}

	public void setSaleprice1(float saleprice1) {
		this.saleprice1 = saleprice1;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getUserids() {
		return userids;
	}

	public void setUserids(String userids) {
		this.userids = userids;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

}
